package com.bit.cfdi.web.validators.common;

import org.zkoss.bind.ValidationContext;
import org.zkoss.bind.validator.AbstractValidator;

import com.bit.cfdi.web.constants.CFDIConstants;

public class EmailValidator extends AbstractValidator {
	
	private String emailName;

	public EmailValidator(final String emailName) {
		this.emailName = emailName;
	}
	
	@Override
	public void validate(final ValidationContext ctx) {
		String emailToValidate = (String) ctx.getProperties(emailName)[0]
				.getValue();
		if(emailToValidate == null || emailToValidate.trim().isEmpty()) {
			return;
		}
		if(!emailToValidate.matches(CFDIConstants.EMAIL_PATTERN)) {
			addInvalidMessage(ctx, emailName, "Email invalido");
		}
	}

}
